<?php
set_time_limit(0);
$root=preg_replace('/cron$/', '', dirname(__FILE__));

require_once($root."inc.config.php");

require_once($root."functions/common/common.php");

require_once($root."functions/general_function.php");

require_once($root."functions/downline/downline_function.php");

require_once($root."functions/comm/comm_function.php");

require_once($root."initialize.php");

if(($db_msg=cron_initialize()) !== true){

	$errmsg="Error starting rebuild tree";

	$a_sub="Error starting rebuild tree";

	$a_msg="<p>Dear admin,</p>$errmsg";

	//email_admin($a_sub, $a_msg, 'e');

}else{

	$db=$vars["db"];

	global $vars;
 	$db=$vars["db"];

	$sql = "SELECT uid FROM $db->matrix_fourth WHERE lvl_1>0 AND lvl1_1>0 AND lvl1_2>0 AND ((lvl2_1>0 AND lvl2_2>0 AND lvl2_4>0 AND lvl2_5>0) OR (lvl2_4>0 AND lvl2_5>0 AND lvl2_7>0 AND lvl2_8>0) OR (lvl2_1>0 AND lvl2_2>0 AND lvl2_7>0 AND lvl2_8>0)) AND is_qualify=0";
	$q = mysql_query($sql);							
	if(mysql_num_rows($q)>0){
		if(mysql_num_rows($q)>0){
			while ($complete_matrix = mysql_fetch_assoc($q)) {
				$user_id= $complete_matrix["uid"];
			}
		}
	}else{
		$user_id=0;
	}

	if($user_id>0){
		$atree=array();
		array_push($atree,'1');
		
		while(sizeof($atree)) {
	
			$node=array_shift($atree);
		 	$nd=0;
			$np=1;
	
			$strsq1="select id,fifth_position from $db->users where fifth_upline=".$node." order by fifth_position";
			$rs = mysql_query($strsq1);	
	
			while ($member = mysql_fetch_assoc($rs)) {	
				array_push($atree,$member["id"]);
				$nd++;
				print_r($member["id"]."<br>");

				if($np==$member["fifth_position"] or $member["fifth_position"]==0) $np++;
			}

			if($np<=3 && $node<>'0') break;		
		}

			if($np<=3 && $node<>'0') {
				$sql = "UPDATE $db->users SET fifth_upline=".$node.",fifth_position=".$np.",fifth_date='".date('Y-m-d')."' WHERE id=".$user_id;
				print_r($sql."<br>");//mysql_query($sql);
				
				//own point
				$sql = "UPDATE $db->matrix_fifth SET lvl_1=".$user_id.",date_1='".date('Y-m-d')."' WHERE uid=".$user_id;
				print_r($sql."<br>");//mysql_query($sql);
				$sql = "UPDATE $db->matrix_fourth SET is_qualify=1 WHERE uid=".$user_id;				
				print_r($sql."<br>");//mysql_query($sql);
				
				//first point
				$sql = "SELECT * FROM $db->matrix_fifth WHERE uid=".$node; print_r($sql."<br>");
				$sponsor_matrix=@mysql_fetch_assoc(mysql_query($sql));
				if($sponsor_matrix['lvl1_1']=='0' && $np=='1'){
					$sql = "UPDATE $db->matrix_fifth SET lvl1_1=".$user_id.",date1_1='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}elseif($sponsor_matrix['lvl1_2']=='0' && $np=='2'){
					$sql = "UPDATE $db->matrix_fifth SET lvl1_2=".$user_id.",date1_2='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}elseif($sponsor_matrix['lvl1_3']=='0' && $np=='3'){
					$sql = "UPDATE $db->matrix_fifth SET lvl1_3=".$user_id.",date1_3='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}else{ $sql = "1)".$sql; }
				print_r($sql."<br>");//mysql_query($sql);
				
				//second point
				$sql = "SELECT * FROM $db->matrix_fifth WHERE lvl1_1=".$node; print_r($sql."<br>");
				$q = mysql_query($sql);							
				if(mysql_num_rows($q)>0){
					$sponsor_matrix=@mysql_fetch_assoc($q);
					if($sponsor_matrix['lvl2_1']=='0' && $np=='1'){
						$sql = "UPDATE $db->matrix_fifth SET lvl2_1=".$user_id.",date2_1='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
					}elseif($sponsor_matrix['lvl2_2']=='0' && $np=='2'){
						$sql = "UPDATE $db->matrix_fifth SET lvl2_2=".$user_id.",date2_2='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
					}elseif($sponsor_matrix['lvl2_3']=='0' && $np=='3'){
						$sql = "UPDATE $db->matrix_fifth SET lvl2_3=".$user_id.",date2_3='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
					}else{ $sql = "2)".$sql; }
					print_r($sql."<br>");//mysql_query($sql);
				}
				//second point
				$sql = "SELECT * FROM $db->matrix_fifth WHERE lvl1_2=".$node; print_r($sql."<br>");
				$q = mysql_query($sql);							
				if(mysql_num_rows($q)>0){
					$sponsor_matrix=@mysql_fetch_assoc($q);
					if($sponsor_matrix['lvl2_4']=='0' && $np=='1'){
						$sql = "UPDATE $db->matrix_fifth SET lvl2_4=".$user_id.",date2_4='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
					}elseif($sponsor_matrix['lvl2_5']=='0' && $np=='2'){
						$sql = "UPDATE $db->matrix_fifth SET lvl2_5=".$user_id.",date2_5='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
					}elseif($sponsor_matrix['lvl2_6']=='0' && $np=='3'){
						$sql = "UPDATE $db->matrix_fifth SET lvl2_6=".$user_id.",date2_6='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
					}else{ $sql = "3)".$sql; }
					print_r($sql."<br>");//mysql_query($sql);
				}
				//second point
				$sql = "SELECT * FROM $db->matrix_fifth WHERE lvl1_3=".$node; print_r($sql."<br>");
				$q = mysql_query($sql);
				if(mysql_num_rows($q)>0){
					$sponsor_matrix=@mysql_fetch_assoc($q);
					if($sponsor_matrix['lvl2_7']=='0' && $np=='1'){
						$sql = "UPDATE $db->matrix_fifth SET lvl2_7=".$user_id.",date2_7='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
					}elseif($sponsor_matrix['lvl2_8']=='0' && $np=='2'){
						$sql = "UPDATE $db->matrix_fifth SET lvl2_8=".$user_id.",date2_8='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
					}elseif($sponsor_matrix['lvl2_9']=='0' && $np=='3'){
						$sql = "UPDATE $db->matrix_fifth SET lvl2_9=".$user_id.",date2_9='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
					}else{ $sql = "4)".$sql; }
					print_r($sql."<br>");//mysql_query($sql);
				}
			}
exit;
		//check full matrix
		$sql = "SELECT uid FROM $db->matrix_fifth WHERE lvl_1>0 AND lvl1_1>0 AND lvl1_2>0 AND lvl1_3>0 AND lvl2_1>0 AND lvl2_2>0 AND lvl2_3>0 AND lvl2_4>0 AND lvl2_5>0 AND lvl2_6>0 AND lvl2_7>0 AND lvl2_8>0 AND lvl2_9>0 AND is_qualify=0";
		$q = mysql_query($sql);							
		if(mysql_num_rows($q)>0){
			$complete_matrix=@mysql_fetch_assoc($q);
			while ($complete_matrix = mysql_fetch_assoc($q)) {
				auto_matrix6($complete_matrix["uid"]);
				mysql_query("UPDATE $db->matrix_fifth SET is_qualify=1 WHERE uid=".$complete_matrix["uid"]);
			}
		}
	}
}

?>